package com.bys.system.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bys.response.Result;
import com.bys.system.model.SysMenuModel;
import com.bys.system.model.SysRoleMenuModel;
import com.bys.system.model.SysRoleModel;
import com.bys.system.service.ISysMenuService;
import com.bys.system.service.ISysRoleMenuService;
import com.bys.system.vo.SysMenuVo;
import com.bys.system.vo.sysRoleMenuVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author bys
 * @since 2023-03-31
 */
@Api(tags = "菜单接口")
@RestController
@RequestMapping("/system/sysMenu")
public class SysMenuController {
    @Resource
    private ISysMenuService sysMenuService;
    @Resource
    private ISysRoleMenuService sysRoleMenuService;
    @ApiOperation(value = "新增菜单")
    @PostMapping("/save")
    @Transactional(rollbackFor = Exception.class)
    public Result save(@RequestBody SysMenuModel sysMenuModel){
        sysMenuService.save(sysMenuModel);
        return Result.success();
    }
    @ApiOperation(value = "更新用户")
    @PutMapping("/update")
    @Transactional(rollbackFor = Exception.class)
    public Result update(@RequestBody SysMenuModel sysMenuModel){
        sysMenuService.updateById(sysMenuModel);
        return Result.success();
    }
    @ApiOperation(value = "使用id 查询菜单信息")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id){
        SysMenuModel sysMenuModel = sysMenuService.getById(id);
        return Result.success(sysMenuModel);
    }
    @ApiOperation(value = "删除菜单")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id){
        sysMenuService.deleteLogic(id);
        return Result.success();
    }
    @ApiOperation(value = "查询所有菜单")
    @GetMapping("/menu")
    public Result Menu(){
        List<SysMenuVo> sysMenuModels = sysMenuService.getMenu();
        return Result.success(sysMenuModels);
    }
    @ApiOperation(value = "根据角色获取菜单",notes = "参数： roleId")
    @GetMapping("/roleMenu/{roleId}")
    public Result findRoleMenu(@PathVariable Long roleId){
        List<SysMenuVo> roleMenu =  sysMenuService.findRoleMenu(roleId);
        List<SysMenuVo> allMenu = sysMenuService.getMenu();
        HashMap<String,List<SysMenuVo>> map = new HashMap<String,List<SysMenuVo>>(){{put("roleMenu",roleMenu);put("allMenu",allMenu);}};
        return Result.success(map);
    }
    @ApiOperation(value = "保存角色所属菜单",notes = "参数： roleId,menuId")
    @PostMapping("/saveRoleMenu")
    @Transactional(rollbackFor = Exception.class)
    public Result saveRoleMenu(@RequestBody sysRoleMenuVo sysRoleMenuVo){
        List<Long> menuIds = sysRoleMenuVo.getMenuIds();
        Long roleId = sysRoleMenuVo.getRoleId();
        //删除全部的权限
        sysRoleMenuService.remove(Wrappers.lambdaQuery(SysRoleMenuModel.class).eq(SysRoleMenuModel::getRoleId,roleId));
    //    然后增加
        menuIds.forEach(menuId ->{
            SysRoleMenuModel sysRoleMenuModel = new SysRoleMenuModel();
            sysRoleMenuModel.setMenuId(menuId);
            sysRoleMenuModel.setRoleId(roleId);
            sysRoleMenuService.save(sysRoleMenuModel);
        });
        return Result.success();
    }

}
